package com.example.touch.mediator;

import java.util.ArrayList;
import java.util.Collection;

import com.example.touch.communication.ILocationPersistentAdapter;
import com.example.touch.communication.MockLocationAdapter;
import com.example.touch.communication.entities.CloseByEvent;
import com.example.touch.communication.entities.Event;
import com.example.touch.communication.entities.RoomEvent;
import com.example.touch.communication.entities.TimeTable;
import com.example.touch.communication.entities.Transition;
import com.example.touch.communication.entities.TransitionDestination;
import com.example.touch.communication.entities.Trigger;
import com.example.touch.domain.EventTrigger;
import com.example.touch.location.IEvaluableObjectLocation;

/**
*
* @author pla
*/
public class EventTriggerMapper 
{
	private Collection<TimeTable> timeTables;
	private LocationsRepository locationsRepository;
	
	public EventTriggerMapper(Collection<TimeTable> timeTables)
	{
		this.timeTables = timeTables;
		ILocationPersistentAdapter adapter = new MockLocationAdapter();
		this.locationsRepository = new LocationsRepository(adapter);
	}
	
	public Collection<EventTrigger> mapEventTriggers(Event e)
	{
		Collection<EventTrigger> eventTriggers = new ArrayList<EventTrigger>();
		
		for(Trigger trigger : e.getTriggers().getTriggers())
		{
			Class<?> triggerClass = trigger.getClass();
			
			EventTrigger eventTrigger = null;
			
			if(triggerClass == CloseByEvent.class)
			{
				CloseByEvent closeByEvent = (CloseByEvent)trigger;
				eventTrigger = toEventTrigger(closeByEvent);
			}
			else if (triggerClass == RoomEvent.class)
			{
				RoomEvent roomEvent = (RoomEvent)trigger;
				eventTrigger = toEventTrigger(roomEvent);
			}
			else if (triggerClass == Transition.class)
			{
				Transition transitionEvent = (Transition)trigger;
				eventTrigger = toEventTrigger(transitionEvent);
			}
			
			if(eventTrigger != null)
				eventTriggers.add(eventTrigger);
		}
		
		return eventTriggers;
	}
	
	private EventTrigger toEventTrigger(CloseByEvent closeByEvent)
	{
		String areaID = closeByEvent.getAreaID();
		IEvaluableObjectLocation location = resolveLocationReference(areaID);
		int radius = closeByEvent.getRadius();
		int tolerance = closeByEvent.getTolerance();
		String timeTableId = closeByEvent.getTimeTable();
		TimeTable timeTable = getTimeTable(timeTableId);
		
		return new EventTrigger(radius, tolerance, location, timeTable);
	}
	
	private EventTrigger toEventTrigger(RoomEvent roomEvent)
	{
		String roomID = roomEvent.getRoomID();
		IEvaluableObjectLocation location = resolveLocationReference(roomID);
		String timeTableID = roomEvent.getTimeTable();
		TimeTable timeTable = getTimeTable(timeTableID);
		
		return new EventTrigger(0, 0, location, timeTable);
	}
	
	// Not currently supported
	private EventTrigger toEventTrigger(Transition transitionEvent)
	{
		TransitionDestination from = transitionEvent.getFrom();
		TransitionDestination to = transitionEvent.getTo();
		
		EventTrigger fromObj = toEventTrigger(from);
		EventTrigger toObj = toEventTrigger(to);
		
		return null;
	}
	
	private EventTrigger toEventTrigger(TransitionDestination transDest)
	{
		String roomId = transDest.getRoomAreaID();
		IEvaluableObjectLocation location = resolveLocationReference(roomId);
		int radius = transDest.getRadius();
		int tolerance = transDest.getTolerance();
		String timeTableId = transDest.getTimeTable();
		TimeTable timeTable = getTimeTable(timeTableId);
		
		return new EventTrigger(radius, tolerance, location, timeTable);
	}
	
	private TimeTable getTimeTable(String timeTableId)
	{
		for(TimeTable tt : timeTables)
		{
			if(tt.getId().equalsIgnoreCase(timeTableId))
				return tt;
		}
		
		return null;
	}
	
	private IEvaluableObjectLocation resolveLocationReference(String locationId)
	{
		return locationsRepository.getLocation(locationId);
	}
}
